/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.sandbox.customization

import com.almasb.fxgl.animation.*
import com.almasb.fxgl.app.*
import com.almasb.fxgl.dsl.*
import javafx.geometry.*
import javafx.scene.*
import javafx.scene.paint.*
import javafx.scene.shape.*
import javafx.util.*
import java.util.*

/**
 * @author Almas Baimagambetov (almaslvl@gmail.com)
 */
class CustomIntroApp : GameApplication() {
	override fun initSettings(settings: GameSettings) {
		settings.isIntroEnabled = true
		settings.sceneFactory = object : SceneFactory() {
			override fun newIntro(): IntroScene {
				return MyIntroScene()
			}
		}
	}
	
	class MyIntroScene : IntroScene() {
		private val animations = ArrayList<Animation<*>>()
		
		private var index = 0
		
		init {
			val circle1 = Circle(SIZE.toDouble(), SIZE.toDouble(), SIZE.toDouble(), Color.color(0.94, 0.5, 0.73, 0.6))
			val circle2 = Circle(SIZE.toDouble(), SIZE.toDouble(), SIZE.toDouble(), Color.color(0.4, 0.5, 0.33, 0.6))
			circle2.translateX = 300.0
			circle2.translateY = 250.0
			val circle3 = Circle(SIZE.toDouble(), SIZE.toDouble(), SIZE.toDouble(), Color.color(0.14, 0.95, 0.13, 0.6))
			circle3.translateX = 500.0
			circle3.translateY = 50.0
			
			val circles = Group(circle1, circle2, circle3)
			
			circles.children.forEach { c ->
				val anim = FXGL.animationBuilder()
					.duration(Duration.seconds(1.66))
					.delay(Duration.seconds(1.66 * index++))
					.interpolator(Interpolators.EXPONENTIAL.EASE_OUT())
					.scale(c)
					.from(Point2D(0.0, 0.0))
					.to(Point2D(1.0, 1.0))
					.build()
				
				animations.add(anim)
			}
			
			animations[animations.size - 1].onFinished = Runnable { this.finishIntro() }
			
			contentRoot.children.addAll(
				Rectangle(FXGL.getAppWidth().toDouble(), FXGL.getAppHeight().toDouble()),
				circles
			)
		}
		
		override fun onUpdate(tpf: Double) {
			super.onUpdate(tpf)
			animations.forEach { a -> a.onUpdate(tpf) }
		}
		
		override fun startIntro() {
			animations.forEach { a -> a.start() }
		}
		
		companion object {
			private const val SIZE = 150
		}
	}
}


fun main(args: Array<String>) {
	GameApplication.launch(CustomIntroApp::class.java, args)
}

